﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PanelManager : MonoBehaviour
{
	public enum PanelType
	{
		Panel,
		Tip,
	}

	static Dictionary<PanelType, Transform> types = new Dictionary<PanelType, Transform>();
	public static Dictionary<string, BasePanel> panels = new Dictionary<string, BasePanel>();
	public static Transform root;
	public static Transform canvas;


	public static void Init()
	{
		root = GameObject.Find("Root").transform;
		canvas = root.Find("Canvas").transform;

		Transform panel = canvas.Find("Panel");
		Transform tip = canvas.Find("Tip");

		types.Add(PanelType.Panel, panel);
		types.Add(PanelType.Tip, tip);
	}

	public static void Open<T>(params object[] para) where T : BasePanel
	{
		string name = typeof(T).ToString();
		if (panels.ContainsKey(name)) return;

		BasePanel panel = root.gameObject.AddComponent<T>();
		panel.OnInit();
		panel.Init();

		Transform layer = types[panel.type];
		panel.skin.transform.SetParent(layer, false);
		panels.Add(name, panel);
		panel.OnShow(para);
	}

	public static void Close(string name)
	{
		if (!panels.ContainsKey(name)) return;

		BasePanel panel = panels[name];
		panel.OnClose();

		panels.Remove(name);

		Destroy(panel.skin);
		Destroy(panel);
	}
}
